/*     */ package com.zhonglee.register;
/*     */ 
/*     */ import com.zhonglee.util.TimeUtils;
/*     */ import java.io.File;
/*     */ import java.util.Date;
/*     */ 
/*     */ public class SerialCodeUtils
/*     */ {
/*     */   public static String getMachineCode()
/*     */   {
/*  12 */     BaseEncoderUtils coderUtils = new BaseEncoderUtils();
/*     */ 
/*  14 */     String machineString = getWindirDate();
/*     */ 
/*  16 */     String machineBase64 = coderUtils.getBASE64(machineString);
/*     */ 
/*  18 */     String machineConfuse = confuse(machineBase64);
/*  19 */     return machineConfuse;
/*     */   }
/*     */ 
/*     */   public static String generateSerialCode(String machineCode, Date usefulLife)
/*     */   {
/*  24 */     String serialCode = null;
/*     */ 
/*  26 */     BaseEncoderUtils coderUtils = new BaseEncoderUtils();
/*     */ 
/*  28 */     String dest = unConfuse(machineCode);
/*  29 */     dest = coderUtils.getFromBASE64(dest);
/*     */ 
/*  31 */     dest = dest + TimeUtils.getShortTime(usefulLife);
/*     */ 
/*  33 */     serialCode = coderUtils.EncoderString("MD5", dest) + "$" + TimeUtils.getShortTime(usefulLife);
/*     */ 
/*  35 */     return serialCode;
/*     */   }
/*     */ 
/*     */   public static boolean validateSerialCode(String serialCode)
/*     */   {
/*  40 */     if ((serialCode == null) || (serialCode.trim().length() == 0)) {
/*  41 */       return false;
/*     */     }
/*  43 */     BaseEncoderUtils coderUtils = new BaseEncoderUtils();
/*     */ 
/*  45 */     String[] sArray = serialCode.split("[$]");
/*  46 */     if (sArray.length < 2) return false;
/*     */ 
/*  48 */     String machineString = getWindirDate();
/*  49 */     String srcMd5 = coderUtils.EncoderString("MD5", machineString + sArray[1]);
/*     */ 
/*  52 */     if (srcMd5.equals(sArray[0])) {
/*  53 */       return true;
/*     */     }
/*     */ 
/*  56 */     return false;
/*     */   }
/*     */ 
/*     */   public static String getSerialCode(String serialCode) {
/*  60 */     if ((serialCode == null) || (serialCode.trim().length() == 0)) {
/*  61 */       return null;
/*     */     }
/*  63 */     BaseEncoderUtils coderUtils = new BaseEncoderUtils();
/*     */ 
/*  65 */     String[] sArray = serialCode.split("[$]");
/*  66 */     if (sArray.length < 2) return null;
/*     */ 
/*  68 */     String machineString = getWindirDate();
/*  69 */     String srcMd5 = coderUtils.EncoderString("MD5", machineString + sArray[1]);
/*     */ 
/*  72 */     return srcMd5 + "$" + sArray[1];
/*     */   }
/*     */ 
/*     */   public static boolean validateSerialCode2(String serialCode)
/*     */   {
/*  78 */     String[] sArray = serialCode.split("[$]");
/*  79 */     if (sArray.length < 3) return false;
/*  80 */     if (sArray[2].equals("release")) {
/*  81 */       return true;
/*     */     }
/*     */ 
/*  84 */     return false;
/*     */   }
/*     */ 
/*     */   public static Date getUsefulLife(String serialCode)
/*     */   {
/*  90 */     if (serialCode == null) return null;
/*  91 */     String[] sArray = serialCode.split("[$]");
/*  92 */     if (sArray.length < 2) return null;
/*  93 */     Date date = TimeUtils.getDate(sArray[1]);
/*  94 */     return date;
/*     */   }
/*     */ 
/*     */   private static String getWindirDate() {
/*  98 */     String dirName = System.getenv("windir");
/*  99 */     if (dirName == null) {
/* 100 */       throw new IllegalArgumentException("Register fail. Error code is 01.");
/*     */     }
/* 102 */     dirName = dirName + File.separatorChar + "system32" + File.separatorChar + "config";
/*     */ 
/* 104 */     File folder = new File(dirName);
/* 105 */     if (folder.exists()) {
/* 106 */       Date dirDate = new Date();
/* 107 */       dirDate.setTime(folder.lastModified());
/* 108 */       return TimeUtils.getLongTime(dirDate);
/*     */     }
/* 110 */     throw new IllegalArgumentException("Register fail. Error code is 02.");
/*     */   }
/*     */ 
/*     */   private static String confuse(String src)
/*     */   {
/* 116 */     char[] srcChars = src.toCharArray();
/* 117 */     StringBuffer dest = new StringBuffer();
/* 118 */     int length = srcChars.length;
/* 119 */     int loop = 0;
/* 120 */     if (length % 2 == 0)
/* 121 */       loop = length / 2;
/*     */     else {
/* 123 */       loop = (length - 1) / 2;
/*     */     }
/*     */ 
/* 126 */     for (int i = 0; i < loop; i++) {
/* 127 */       dest.append(srcChars[i]).append(srcChars[(i + loop)]);
/*     */     }
/* 129 */     if (length % 2 != 0) {
/* 130 */       dest.append(srcChars[(srcChars.length - 1)]);
/*     */     }
/*     */ 
/* 133 */     return dest.toString();
/*     */   }
/*     */ 
/*     */   private static String unConfuse(String src)
/*     */   {
/* 138 */     char[] srcChars = src.toCharArray();
/* 139 */     StringBuffer dest = new StringBuffer();
/* 140 */     int length = srcChars.length;
/* 141 */     int loop = 0;
/* 142 */     if (length % 2 == 0)
/* 143 */       loop = length / 2;
/*     */     else {
/* 145 */       loop = (length - 1) / 2;
/*     */     }
/*     */ 
/* 148 */     StringBuffer buf1 = new StringBuffer();
/* 149 */     StringBuffer buf2 = new StringBuffer();
/* 150 */     for (int i = 0; i < loop; i++) {
/* 151 */       buf1.append(srcChars[(i * 2)]);
/* 152 */       buf2.append(srcChars[(i * 2 + 1)]);
/*     */     }
/* 154 */     dest.append(buf1).append(buf2);
/* 155 */     if (length % 2 != 0) {
/* 156 */       dest.append(srcChars[(srcChars.length - 1)]);
/*     */     }
/*     */ 
/* 159 */     return dest.toString();
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.zhonglee.register.SerialCodeUtils
 * JD-Core Version:    0.6.2
 */